﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Books.Books
{
    public class BookAppService :
        CrudAppService<
            Book, //The Book entity
            BookDto, //Used to show books
            Guid, //Primary key of the book entity
            QueryBookDto, //Used for paging/sorting
            CreateUpdateBookDto> //Used to create/update a book
    {
        private IRepository<Book, Guid> _repository;
        public BookAppService(IRepository<Book, Guid> repository)
            : base(repository)
        {
            _repository = repository;
        }
        [HttpPost("api/app/book/Add")]
        public int Add(CreateUpdateBookDto input)
        {
            //Dto需要转成Model
            Book model = ObjectMapper.Map<CreateUpdateBookDto, Book>(input);
            _repository.InsertAsync(model);
            return 1;
        }
        protected override async Task<IQueryable<Book>> CreateFilteredQueryAsync(QueryBookDto input)
        {
            return (await base.CreateFilteredQueryAsync(input)).WhereIf(!string.IsNullOrEmpty(input.Name), c => c.Name.Contains(input.Name));
        }
    }
}
